//
//  HJPayManager.m
//  Sneakers
//
//  Created by 胡光健 on 2017/12/14.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJPayManager.h"
#import <AlipaySDK/AlipaySDK.h>
#import "UPPaymentControl.h"

@implementation HJPayManager

+(instancetype)manager {
    static HJPayManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    return manager;
}

-(void)setBody:(NSString *)body {
    _body = body;
}
-(void)setSubject:(NSString *)subject {
    _subject = subject;
}
-(void)setOut_trade_no:(NSString *)out_trade_no {
    _out_trade_no = out_trade_no;
}
-(void)setTotal_amount:(double)total_amount {
    _total_amount = total_amount;
}

-(void)setOrder_type:(NSString *)order_type {
    _order_type =order_type;
}
#pragma mark -- 支付宝支付
-(void)zhifubaoWithPaysuccess:(void (^)(NSDictionary *dic))success failure:(void (^)(NSString *msg))failure {
    //将商品信息赋予AlixPayOrder的成员变量
//    APOrderInfo* order = [APOrderInfo new];
//
//    order.app_id = @"2017120700429263";
//    order.method = @"alipay.trade.app.pay";
//    order.charset = @"utf-8";

//     当前时间点
//    NSDateFormatter* formatter = [NSDateFormatter new];
//    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    order.timestamp = [formatter stringFromDate:[NSDate date]];
//
//    order.version = [CommonTools appVersion];
//    //  sign_type设置
//    order.sign_type = @"RSA";
//
////     NOTE: 商品数据
//    order.biz_content = [APBizContent new];
//    order.biz_content.body = _body;
//    order.biz_content.subject = _subject;
//    order.biz_content.out_trade_no = _out_trade_no; //订单ID（由商家自行制定）
//    order.biz_content.timeout_express = @"30m"; //超时时间设置
//    order.biz_content.total_amount = [NSString stringWithFormat:@"%.2f", _total_amount]; //商品价格
//
////    将商品信息拼接成字符串
//    NSString *orderInfo = [order orderInfoEncoded:NO];
//    NSString *orderInfoEncoded = [order orderInfoEncoded:YES];
//    NSLog(@"orderSpec = %@",orderInfo);
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"order_sn"] = _out_trade_no;
    params[@"type"] = @"2";
    params[@"order_type"] = _order_type;
    [[UIViewController currentViewController] showTextHUDWithPleaseWait];
    [TZHttpTool post:PayAPI params:params success:^(NSDictionary *result) {
        // 应用注册scheme,在AliSDKDemo-Info.plist定义URL types
        [[UIViewController currentViewController] hideTextHud];
        NSString *appScheme = @"SneakerCN";
        // NOTE: 调用支付结果开始支付
        [[AlipaySDK defaultService] payOrder:result[@"parameters"][@"scalar"] fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            if (success) {
                success(resultDic);
            }
        }];
    } failure:^(NSString *msg) {
        if (failure) {
            failure(msg);
        }
    }];
}



#pragma mark -- 微信支付
-(void)wechatPayWithDic:(NSDictionary *)dic {
    PayReq * request = [[PayReq alloc]init];
    request.partnerId = [dic objectForKey:@"partnerid"];
    request.prepayId = [dic objectForKey:@"prepayid"];
    request.nonceStr = [dic objectForKey:@"noncestr"];
    request.package = [dic objectForKey:@"package"];
    UInt32 timeStamp = [[dic objectForKey:@"timestamp"] intValue];
    request.timeStamp = timeStamp;
    request.sign = [dic objectForKey:@"sign"];
    
    // 调用微信
    [WXApi sendReq:request];
}


#pragma mark-- 银联支付
-(void)bankPayWithStartPay:(NSString *)payOrder model:(NSString *)model viewController:(UIViewController *)viewController {
    if (![[UPPaymentControl defaultControl] isPaymentAppInstalled]) {
        [[UIViewController currentViewController] showErrorHUDWithError:@"尚未安装银联支付客户端"];
    }else {
        if (payOrder.length > 0) {
            [[UPPaymentControl defaultControl] startPay:payOrder fromScheme:@"SneakerCN" mode:model viewController:viewController];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"银联参数配置不正确" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
        }
    }
}
@end
